package com.cy.action;

import com.cy.common.BaseAction;
import com.cy.condition.BackUserCondition;
import com.cy.entity.BackUser;
import com.cy.entity.BackUserType;
import com.cy.entity.NormalStatus;
import com.cy.service.BackPurviewService;
import com.cy.service.BackUserService;
import com.cy.session.SessionHelper;
import com.cy.session.SessionUser;
import com.cy.util.Configure;

/**
 * 后台登录
 */
public class BackLogin extends BaseAction{
	
	private BackUserService backUserService;
	private BackPurviewService backPurviewService;
	private String msg;
	
	private BackUserCondition condition;
	private BackUser backUser;
	
	/**
	 *	进入后台登录页面
	 */
	public String index() {
		SessionUser sessionUser=SessionHelper.getSessionUser();
		if(sessionUser==null){
			return SUCCESS;
		}else{
			if(SessionHelper.getSessionUser().getBackUserType()!=BackUserType.systemAdmin){
				backUser=(BackUser) backUserService.getObject(sessionUser.getId());
			}else{
				
			}
			
			return "intoSystem";
		}
		
	}
	
	public String execute() {
		return SUCCESS;
	}
	
	/**
	 * 点击登录
	 */
	public String login(){
		if(condition==null){
			msg="用户名或密码错误,请重新登录或者联系系统管理员";
			return "bad";
		}
		if(condition.getLoginName().equals(Configure.getString("vvmall.superAdmin.loginId"))){
			if(condition.getPassword().equals(Configure.getString("vvmall.superAdmin.cyPassWord"))){
				SessionUser sessionUser=new SessionUser();
				sessionUser.setNickname("系统管理员");
				sessionUser.setBackUserType(BackUserType.systemAdmin);
				SessionHelper.setBackUser(sessionUser);
				sessionUser.setPurviews(backPurviewService.listAllPurviewForSysAdmin());
				return "goIndex";
			}else{
				msg="用户名或密码错误,请重新登录或者联系系统管理员";
				return "bad";
			}
		}
		BackUser backUser=backUserService.findBackUserByLoginNameAndPassWord(condition.getLoginName(), condition.getPassword());
		if(backUser==null){
			msg="用户名或密码错误,请重新登录或者联系系统管理员";
			return "bad";
		}else{
			if(backUser.getStatus()==NormalStatus.DELETE){
				msg="用户账户被<span style='color:red'>【禁用】</span> 请联系系统管理员";
				return "bad";
			}
		}
		
		SessionUser sessionUser=new SessionUser();
		sessionUser.setBackuserNo(backUser.getBackuserNo());
		sessionUser.setId(backUser.getId());
		sessionUser.setNickname(backUser.getLoginName());
		sessionUser.setPassword(backUser.getPassword());
		sessionUser.setPurviews(backUserService.listBackUserAllPurview(backUser.getId()));
		SessionHelper.setBackUser(sessionUser);
		
		return "goIndex";
		
	}

	public String loginOut(){
		SessionHelper.removeSession(SessionUser.SESSION_BACKUSER_KEY);
		return SUCCESS;
	}
	
	public BackUserCondition getCondition() {
		return condition;
	}

	public void setCondition(BackUserCondition condition) {
		this.condition = condition;
	}

	public BackUserService getBackUserService() {
		return backUserService;
	}

	public void setBackUserService(BackUserService backUserService) {
		this.backUserService = backUserService;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public BackUser getBackUser() {
		return backUser;
	}

	public void setBackUser(BackUser backUser) {
		this.backUser = backUser;
	}

	public BackPurviewService getBackPurviewService() {
		return backPurviewService;
	}

	public void setBackPurviewService(BackPurviewService backPurviewService) {
		this.backPurviewService = backPurviewService;
	}
	
	

}
